package com.sk.weichat.ui.account;

import android.content.Context;
import android.text.TextUtils;

import com.sk.weichat.db.dao.FriendDao;
import com.sk.weichat.util.PreferenceUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lzs
 * @date 2019/9/23
 * Email:343067508@qq.com
 **/
public class MultiAccountHelper {

    private static boolean ADD_ACCOUNT_FLAG = false;

    private final static String IDs = "MultiAccountHelper_IDS";
    // private final static String ADD_ACCOUNT_FLAG = "MultiAccountHelper_ADD_ACCOUNT_FLAG";

    /**
     * 记录多账号的关系
     *
     * @param userids
     */
    public static final void setMultiAccountIDs(Context context, String[] userids) {
        if (userids == null) {
            return;
        }
        String ids = "";
        for (int i = 0; i < userids.length; i++) {
            ids += userids[i] + ",";
        }
        PreferenceUtils.putString(context, IDs, ids);
    }

    public static final void setMultiAccountIDs(Context context, List<String> userids) {
        if (userids == null) {
            return;
        }
        String ids = "";
        for (int i = 0; i < userids.size(); i++) {
            ids += userids.get(i) + ",";
        }
        PreferenceUtils.putString(context, IDs, ids);
    }


    public static final String[] getMultiAccountIDs(Context context) {
        String ids = PreferenceUtils.getString(context, IDs);
        if (!TextUtils.isEmpty(ids)) {
            String[] idArray = ids.split(",");
            return idArray;
        } else {
            return null;
        }
    }

    public static final List<String> getMultiAccountIDList(Context context) {
        String ids = PreferenceUtils.getString(context, IDs);
        List<String> list = new ArrayList<>();
        if (!TextUtils.isEmpty(ids)) {
            String[] idArray = ids.split(",");
            for (int i = 0; i < idArray.length; i++) {
                list.add(idArray[i]);
            }
        }
        return list;
    }


    public static final void addAccountID(Context context, String newID) {
        List<String> ids = getMultiAccountIDList(context);
        if (!ids.contains(newID)) {
            ids.add(newID);
        }
        setMultiAccountIDs(context, ids);
    }

    public static final boolean removeAccountID(Context context, String newID) {
        List<String> ids = getMultiAccountIDList(context);
        if (ids.contains(newID)) {
            ids.remove(newID);
            setMultiAccountIDs(context, ids);
            return true;
        } else {
            return false;
        }
    }


    public static final void clearMultiAccount(Context context) {
        PreferenceUtils.putString(context, IDs, "");
    }


    public static void setAddAccountFlag(boolean flag) {
        ADD_ACCOUNT_FLAG = flag;
    }

    public static boolean getAddAccountFlag() {
        return ADD_ACCOUNT_FLAG;
        // return PreferenceUtils.getBoolean(context, ADD_ACCOUNT_FLAG, false);
    }

    /**
     *
     * 使用切换账号登录需调用登录成功后保存登录的userid
     * @param context
     * @param userID
     */
    public static void onLoginSuccess(Context context, String userID) {
        FriendDao.getInstance().checkSystemFriend(userID); // 检查 两个公众号
        if (MultiAccountHelper.getAddAccountFlag()) {
            if (!MultiAccountHelper.getMultiAccountIDList(context).contains(userID)) {
                MultiAccountHelper.addAccountID(context, userID);
            }
        } else {
            if (!MultiAccountHelper.getMultiAccountIDList(context).contains(userID)) {
                MultiAccountHelper.clearMultiAccount(context);
                MultiAccountHelper.addAccountID(context, userID);
            }
        }
    }
}
